package com.seal.yoyoshop.web;

import com.seal.yoyoshop.bin.Order;
import com.seal.yoyoshop.bin.Type;
import com.seal.yoyoshop.bin.User;
import com.seal.yoyoshop.service.OrderService;
import com.seal.yoyoshop.service.TypeService;
import com.seal.yoyoshop.service.impl.OrderServiceImpl;
import com.seal.yoyoshop.service.impl.TypeServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "OrderServlet", value = "/index/order")
public class OrderServlet extends HttpServlet {

    private TypeService typeService = new TypeServiceImpl();
    private OrderService orderService = new OrderServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if(user == null){
            response.sendRedirect("login");
        }else {
            request.setAttribute("flag",3);
            List<Type> typeList = typeService.getTypeList();
            request.setAttribute("typeList",typeList);

//            根据用户id查订单
            List<Order> orderList = orderService.getOrderByUserId(user.getId());
            if (orderList.size() != 0) {
                request.setAttribute("orderList", orderList);
            }

            request.getRequestDispatcher("/index/order.jsp").forward(request, response);

        }

    }
}
